package com.atsmart.app.utils;

import java.util.List;

import com.atsmart.app.BizApp;


import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
 /**
  * TODO 通过手机信号获取基站信息
  */
public class LocationUtils {
	private TelephonyManager manager;
	private GsmCellLocation gsmLocation;
	private CdmaCellLocation cdmaLocation;
	/**
	 * Cell Identity，基站编号；
	 */
	private int cellId;
	/**
	 * Mobile Country Code 移动国家代码（中国的为460）
	 */
	private String mcc;
	/**
	 * Mobile Network code 移动网络号码（中国移动为0，中国联通为1，中国电信为2）； 
	 */
	private String mnc;
	/**
	 * Location Area Code，位置区域码
	 */
	private int loc;
	/**
	 * mobileNetworkCode,系统标识
	 */
	private int sid = 0;
	private String operator;
	private int type;
	
	private static LocationUtils instance;
	
	public synchronized static LocationUtils getInstance() {
		if(null == instance){
			instance = new LocationUtils();
		}
		return instance;
	}
	
	/**
	 * @return 返回字符串格式为  mcc, mnc, loc, cellid
	 */
	private int max = 0;
	private int index = 0;
	public String getLocationMsg(){
		try {
			if(null == manager){
				manager = (TelephonyManager) BizApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
			}
			type = manager.getNetworkType();
			//：仅当用户已在网络注册时有效。在CDMA网络中结果也许不可靠。
			operator = manager.getNetworkOperator();
			
			if(StringUtil.isEmptyOrNull(operator)){
				//获取SIM卡提供的移动国家码和移动网络码.5或6位的十进制数字.SIM卡的状态必须是SIM_STATE_READY(使用getSimState()判断)
//				if(manager.getSimState() == TelephonyManager.SIM_STATE_READY){
					operator = manager.getSimOperator();
//				}
			}
			
			if(!StringUtil.isEmptyOrNull(operator)){
				mcc = operator.substring(0, 3);
				mnc = operator.substring(3);
			}
			if(TelephonyManager.NETWORK_TYPE_EVDO_A == type || TelephonyManager.NETWORK_TYPE_CDMA == type || TelephonyManager.NETWORK_TYPE_1xRTT == type){
				cdmaLocation = (CdmaCellLocation) manager.getCellLocation();
				cellId = cdmaLocation.getBaseStationId();
				loc = cdmaLocation.getNetworkId();
				sid = cdmaLocation.getSystemId();

			}else if(TelephonyManager.NETWORK_TYPE_EDGE == type){
				gsmLocation = (GsmCellLocation) manager.getCellLocation();
				cellId = gsmLocation.getCid();
				loc = gsmLocation.getLac();
				sid = 0;
				try {
					List<NeighboringCellInfo> info = manager.getNeighboringCellInfo();
					if (info.size() > 0) {
						max = info.get(0).getRssi() * 2 - 113;
						for (int i = 0; i < info.size(); i++) {
							if (max < (info.get(i).getRssi() * 2 - 113)) {
								max = (info.get(i).getRssi()) * 2 - 113;
								index = i;
							}
						}
						//最近基站CELL-id
						if(info.get(index).getCid() > 0 && info.get(index).getLac() > 0){
							cellId = info.get(index).getCid();
							loc = info.get(index).getLac();
						}
					}
				} catch (Exception e) {
					Log.e("cell", "相邻基站异常");
				}
			}else if(TelephonyManager.NETWORK_TYPE_GPRS == type ||TelephonyManager.NETWORK_TYPE_HSDPA == type){
				gsmLocation = (GsmCellLocation) manager.getCellLocation();
				cellId = gsmLocation.getCid();
				loc = gsmLocation.getLac();
				sid = 0;
				try {
					List<NeighboringCellInfo> info = manager.getNeighboringCellInfo();
					if (info.size() > 0) {
						max = info.get(0).getRssi() * 2 - 113;
						for (int i = 0; i < info.size(); i++) {
//							Log.e("TAG", "信号轻度： = "+info.get(i).getRssi() + " lac: = " + info.get(i).getLac() + " cellId: = " + info.get(i).getCid());
							if (max < (info.get(i).getRssi() * 2 - 113)) {
								max = (info.get(i).getRssi()) * 2 - 113;
								index = i;
							}
						}
						//最近基站cell-id
						if(info.get(index).getCid() > 0 && info.get(index).getLac() > 0){
							cellId = info.get(index).getCid();
							loc = info.get(index).getLac();
						}
					 
					}
				} catch (Exception e) {
					Log.e("cell", "相邻基站异常");
				}
			
			}else{
				gsmLocation = (GsmCellLocation) manager.getCellLocation();
				cellId = gsmLocation.getCid();
				loc = gsmLocation.getLac();
				sid = 0;
			}
			if(BizApp.isWrite){
				Log.e("基站信息", "loc = " + loc + " , cellId = " + cellId + " , sid = " + sid);
			}
			
			if((loc < 0) || (cellId < 0)){
				return  0 + "," + 0 + "," + 0 + "," + 0 + "," + 0;
			}else{
				return  mcc + "," + mnc + "," + loc + "," + cellId + "," + sid;
			}
		} catch (Exception e) {
			return 0 + "," + 0 + "," + 0 + "," + 0 + "," + 0;
		}
	}
}
